<template>
  <div class="app-container">
    <el-alert
      :closable="false"
      style="margin-bottom: 20px"
      title="PS：以下角色信息仅供超级管理员参考，如需增添新的角色信息请联系系统开发人员！"
      type="warning">
    </el-alert>
    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;">
      <el-table-column label="ID" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="角色代号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="角色名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name_cn }}</span>
        </template>
      </el-table-column>
      <el-table-column label="说明" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.desc }}</span>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="dialogStatus === 'create' ? '添加账号' : '编辑账号'" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="form" label-position="left" label-width="120px" style="width: 400px; margin-left:50px;">
        <el-form-item label="账号名称" prop="name">
          <el-input v-model="form.name"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"/>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password"/>
        </el-form-item>
        <el-form-item label="确认密码" prop="password_confirm">
          <el-input v-model="form.password_confirm" type="password"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, updateAccount, createAccount, deleteAccount } from '@/api/setting'

export default {
  name: 'RoleList',
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: '',
        type: undefined
      },
      form: {
        name: undefined,
        email: undefined,
        password: undefined,
        password_confirm: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      rules: {
        name: [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, message: '密码长度不小于6位', trigger: 'blur' }
        ],
        password_confirm: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getRoleList().then(response => {
        this.list = response.data.data
      }).catch(err => {
        console.log(err)
      }).finally(() => {
        this.listLoading = false
      })
    },
    resetTemp() {
      this.form = {
        name: undefined,
        email: undefined,
        password: undefined,
        password_confirm: undefined
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.form.password_confirm !== this.form.password) {
            this.$message.error('密码不一致')
            return
          }
          createAccount(this.form).then(response => {
            if (response.status === 200 && response.data.status === 400) {
              this.$message.error(response.data.message)
            } else {
              console.log(response)
              this.getList()
              this.dialogFormVisible = false
              this.$message.success('添加成功')
            }
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          updateAccount(tempData).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$message.success('更新成功')
          })
        }
      })
    },
    handleDelete(row) {
      const id = row.id
      this.$confirm('此操作将永久删除该账号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAccount(id).then(response => {
          if (response.status === 204) {
            this.$message.success('删除成功')
            this.getList()
          }
        })
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
  .success {
    color: #67C23A;
  }
</style>
